﻿using System;
using System.ComponentModel;
using System.Windows.Controls;
using System.Windows.Input;
using Achievements;
using Microsoft.Phone.Controls;

namespace AchievementSample
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }

        private void AchievementPageTitle(object sender, MouseButtonEventArgs e)
        {
            //PressedAppTitleAchievement is defined as being clicked once. (ActivityAchievement)
            if (!AppAchievements.Instance.PressedAppTitleAchievement.IsUnlocked)
            {
                AppAchievements.Instance.PressedAppTitleAchievement.Unlock();
            }
            //AchievementHungry is defined as being clicked more than once (an IncrementableAchievement)
            if (!AppAchievements.Instance.AchievementHungryAchievement.IsUnlocked)
            {
                AppAchievements.Instance.AchievementHungryAchievement.Increment();
            }
        }

        protected override void OnBackKeyPress(CancelEventArgs e)
        {
            //closed app achievement is being defined as clicked once. (ActivityAchievement)
            if (!AppAchievements.Instance.ClosedAppAchievement.IsUnlocked)
            {
                AppAchievements.Instance.ClosedAppAchievement.Unlock();
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }

        private void LockedTitle(object sender, MouseButtonEventArgs e)
        {
            //HeaderAchievement is defined as clicking the locked and unlcoked headers within a duration.
            if (!AppAchievements.Instance.HeaderAchievement.IsUnlocked)
            {
                //start the timer.
                AppAchievements.Instance.HeaderAchievement.StartTimer();
            }
        }

        private void UnlockedTitle(object sender, MouseButtonEventArgs e)
        {
            if (!AppAchievements.Instance.HeaderAchievement.IsUnlocked)
            {
                //check if starttimer is set.
                if (AppAchievements.Instance.HeaderAchievement.StartTime.HasValue)
                {
                    //if set, mark that this transaction is completed (i.e. user started and completed successfully)
                    AppAchievements.Instance.HeaderAchievement.CompletedSuccessfully();
                }
            }
        }

        private void ListBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var lb = sender as ListBox;
            if (lb == null || lb.SelectedIndex == -1) return;

            var achievement = lb.SelectedItem as Achievement;
            int idx = AppAchievements.Instance.AllAchievements.IndexOf(achievement);

            NavigationService.Navigate(new Uri("/ItemPage.xaml" + "?idx=" + idx, UriKind.Relative));
            lb.SelectedIndex = -1;
        }
    }
}